<template>
  <div class="app-container">
    <div class="title">讲师列表</div>
    <el-form :inline="true" :model="teacherQuery" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="teacherQuery.name" placeholder="讲师名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="teacherQuery.level" placeholder="讲师头衔">
          <el-option label="高级讲师" :value="1"></el-option>
          <el-option label="首席讲师" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="添加时间">
        <el-date-picker v-model="teacherQuery.begin" type="date" placeholder="选择开始日期">
        </el-date-picker>
         <el-date-picker v-model="teacherQuery.end" type="date" placeholder="选择截止日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
        <el-button @click="resetData()">清空</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row>
      <el-table-column align="center" label="序号" width="70">
        <template slot-scope="scope">
          {{(page-1)*limit+ scope.$index+1 }}
        </template>
      </el-table-column>
      <el-table-column label="名称" width="70" prop="name">
      
      </el-table-column>
      <el-table-column label="头衔" width="80" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.level===1?"高级讲师":"首席讲师"}}</span>
        </template>
      </el-table-column>
      <el-table-column label="资历" prop="intro">
       
      </el-table-column>
      <el-table-column class-name="status-col" label="添加时间" width="160" align="center" prop="gmtCreate">
        
      </el-table-column>
            <el-table-column class-name="status-col" label="排序" width="60" align="center" prop="sort">
        
      </el-table-column>
      <!-- <el-table-column align="center" prop="created_at" label="排序"  width="60">
        <template slot-scope="scope">
          <span>{{ scope.row.sort }}</span>
        </template> -->
      <!-- </el-table-column> -->

       <el-table-column
      fixed="right"
      label="操作"
      width="200"  align="center">
      <template slot-scope="scope">
        <router-link :to="`/teacher/edit/${scope.row.id}`">
        <el-button  type="primary" size="mini" icon="el-icon-edit">修改</el-button>
        </router-link>
        
        <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteTeacher(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination style="text-align: center; margin-top:20px ;"
      @current-change="getList"
      :current-page="page"
      :page-size="limit"
      layout="total, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>
<script>
import teacherApi from '@/api/teacher'
export default {
  data() {
    return {
      list: null,//当前列表所需数据
      listLoading: true,//转菊花效果
      page:1,//当前页
      limit:10,//每页多少数据
      teacherQuery:{},//条件查询对象
      total:0//总条数
    };
  },
  created(){
    this.getList()
  },
  methods: {
    getList(page=1){
      // console.log(page)
      // 获取当前页码
      this.page=page

      //调用讲师列表Api
      this.listLoading=true
      teacherApi.getList(this.page,this.limit,this.teacherQuery).then(res=>{
        // 获取API数据
        this.list=res.data.items

        // 获取总条数
        this.total=res.data.totalCount
        // 让转菊花效果停止
      this.listLoading=false
      })
    },
    // 清空查询数据
    resetData(){
      // 将查询表单对象内容清空
      this.teacherQuery={}
      // 调用讲师API接口
      this.getList()
    },

    // 根据id删除讲师
    deleteTeacher(id){
// console.log(id)
           this.$confirm('您确定要删除该讲师吗?', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
        teacherApi.deleteById(id).then(res=>{
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList()
        })
        }).catch(() => {
            this.$message({
            type: 'info',
            message: '已取消删除'
          }); 
        });

    }
  },
};
</script>
<style scoped>
.title {
  font-weight: 700;
  font-size: 22px;
  color: #a25900;
  margin-bottom: 15px;
}
</style>